package com.jc.service.h5game;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.GameUser;
import com.jc.ds.model.UserToken;
import com.jc.ds.model.UserVrtAddress;
import com.jc.framework.enums.Booleans;
import com.jc.service.gameinterface.GameInterfaceService;
import com.jc.service.gameuser.GameUserService;
import com.jc.service.usertoken.UserTokenService;
import com.jc.service.uservrtaddress.UserVrtAddressService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@SuppressWarnings("all")
@Service
public class H5GameServiceImpl implements H5GameService {

    @Autowired
    private GameInterfaceService gameInterfaceService;
    @Autowired
    private GameUserService gameUserService;
    @Autowired
    private UserVrtAddressService userVrtAddressService;
    @Autowired
    private UserTokenService userTokenService;
    @Value("${vrt.gamemoney.ratio}")
    private String vrtGamemoneyRatio;
    @Value("${vrt.transfer.fee}")
    private String vrtTransferFee;


    @Override
    public Map<String, Object> platformInitialization(Map<String, Object> param) {
        //本地化用户数据
        GameUser gameUser = WebUtils.mapToBean(param, new GameUser());
        GameUser quGameUser = new GameUser();
        quGameUser.setUserId(gameUser.getUserId());
        List<GameUser> gameUsers = gameUserService.queryAll(quGameUser);
        if (CollectionUtils.isEmpty(gameUsers)){
            //新增
            gameUserService.add(gameUser,false);
            gameUser.setIsAgree(Booleans.FALSE.getName());
        }else{
            //更新
            gameUser.setId(gameUsers.get(0).getId());
            gameUserService.modify(gameUser,false);
            gameUser.setIsAgree(gameUsers.get(0).getIsAgree());
        }
        param.put("vrtGamemoneyRatio",vrtGamemoneyRatio);
        param.put("vrtTransferFee",vrtTransferFee);
        //免责声明
        param.put("isAgree",gameUser.getIsAgree());
        //用户VRT地址
        Boolean addressDefault = false;
        List<Map<String,Object>> userVrtAddress = JSONObject.parseObject(param.get("userVrtAddress").toString(), List.class);
        //遍历地址,保存
        if (userVrtAddress.size() >1 ){
            for (Map<String,Object> vrtAddress:userVrtAddress) {
                UserVrtAddress userVrtAddressIn = new UserVrtAddress();
                userVrtAddressIn.setUserId(gameUser.getUserId());
                userVrtAddressIn.setUserVrtAddress(vrtAddress.get("address").toString());
                //去查询是否存在
                List<UserVrtAddress> userVrtAddresses = userVrtAddressService.queryAll(userVrtAddressIn);
                userVrtAddressIn.setAddressRemark(vrtAddress.get("remark").toString());
                if (CollectionUtils.isEmpty(userVrtAddresses)){
                    userVrtAddressService.add(userVrtAddressIn,false);
                }else{
                    userVrtAddressIn.setSelected(userVrtAddresses.get(0).getSelected());
                    userVrtAddressIn.setId(userVrtAddresses.get(0).getId());
                    //设置默认地址
                    userVrtAddressService.modify(userVrtAddressIn,false);
                }
                if (Boolean.valueOf(userVrtAddressIn.getSelected())){
                    addressDefault = true;
                }
            }
        }else{
            for (Map<String,Object> vrtAddress:userVrtAddress) {
                UserVrtAddress userVrtAddressIn = new UserVrtAddress();
                userVrtAddressIn.setUserId(gameUser.getUserId());
                userVrtAddressIn.setUserVrtAddress(vrtAddress.get("address").toString());
                //去查询是否存在
                List<UserVrtAddress> userVrtAddresses = userVrtAddressService.queryAll(userVrtAddressIn);
                userVrtAddressIn.setAddressRemark(vrtAddress.get("remark").toString());
                if (CollectionUtils.isEmpty(userVrtAddresses)){
                    userVrtAddressIn.setSelected(Booleans.TRUE.getName());
                    userVrtAddressService.add(userVrtAddressIn,false);
                }else{
                    userVrtAddressIn.setSelected(userVrtAddresses.get(0).getSelected());
                    userVrtAddressIn.setId(userVrtAddresses.get(0).getId());
                    //设置默认地址
                    userVrtAddressService.modify(userVrtAddressIn,false);
                }
                if (Boolean.valueOf(userVrtAddressIn.getSelected())){
                    addressDefault = true;
                }
            }
        }
        param.put("addressDefault",addressDefault);
        //生成token表记录
        String token = UUID.randomUUID().toString();
        UserToken userToken = new UserToken();
        userToken.setCreateTime(new Date());
        userToken.setIsDelete(Booleans.FALSE.getName());
        userToken.setUserId(gameUser.getUserId());
        userToken.setUserToken(token);
        userTokenService.add(userToken,false);
        param.put("VrtToken",token);
        return param;
    }
}
